package com.sxj.corejava.code08_常用类.其他类;

import com.sxj.corejava.util.DateUtil;

import java.util.Calendar;
import java.util.Date;

/**
 * @author 石小俊
 * 2024年07月23日 9:34
 */
public class Test01_日历类 {

    public static void main(String[] args) {
        // 由于日历类是一个抽象类,无法通过构造方法创建对象
        // 其提供了一个静态方法,用于生成对应的实例
        // 此时的对象表示的是当前时间所对应的日历
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar);
        Date date = calendar.getTime();
        System.out.println(DateUtil.toString(date));
        calendar.setTime(DateUtil.getYesterday(date));
        System.out.println(DateUtil.toString(calendar.getTime()));
        System.out.println("年份:" + calendar.get(Calendar.YEAR));

        // 根据参数取对应位置的值
        // 月份取值:0-11表示1-12月
        System.out.println("月份:" + (calendar.get(Calendar.MONTH) + 1));
        System.out.println("本月第几天:" + calendar.get(Calendar.DATE));
        System.out.println("本月第几天:" + calendar.get(Calendar.DAY_OF_MONTH));
        System.out.println("小时数:" + calendar.get(Calendar.HOUR));
        System.out.println("分钟数:" + calendar.get(Calendar.MINUTE));
        System.out.println("秒数:" + calendar.get(Calendar.SECOND));
        System.out.println("毫秒数:" + calendar.get(Calendar.MILLISECOND));
        System.out.println("本周第几天:" + calendar.get(Calendar.DAY_OF_WEEK));
        System.out.println("本月第几周:" + calendar.get(Calendar.WEEK_OF_MONTH));
        System.out.println("本年第几周:" + calendar.get(Calendar.WEEK_OF_YEAR));

        System.out.println(DateUtil.toString(calendar.getTime()));
        // 可以修改指定部分的值
        calendar.set(Calendar.YEAR, 2023);
        // 月份:0-11
        calendar.set(Calendar.MONTH, 6 - 1);
        calendar.set(Calendar.DATE, 20);
        calendar.set(Calendar.MONTH, 1);
        calendar.set(Calendar.DATE, 30);
        calendar.set(Calendar.DATE, 0);
        System.out.println(DateUtil.toString(calendar.getTime()));
        calendar.add(Calendar.DATE, 1);
        calendar.add(Calendar.MONTH, -2);
        System.out.println(DateUtil.toString(calendar.getTime()));
        System.out.println("毫秒数:" + calendar.getTimeInMillis());
        System.out.println("毫秒数:" + calendar.getTime().getTime());
        calendar.clear();
        System.out.println(DateUtil.toString(calendar.getTime()));

        System.out.println("2024年2月总天数:" + DateUtil.getDay2(2024, 2));
        System.out.println("2025年2月总天数:" + DateUtil.getDay2(2025, 2));
    }

}
